﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Entities;
using WMS.PurchaseNoticeDetailTable.DTO;

namespace WMS.PurchaseNoticeDetailTable
{
    public class PurchaseNoticeDetailAppService : ApplicationService, IPurchaseNoticeDetailAppService
    {
        private readonly IRepository<PurchaseNoticeDetails, int> _purchasenoticeDetailRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchasenoticeDetailRepository"></param>
        public PurchaseNoticeDetailAppService(IRepository<PurchaseNoticeDetails, int> purchasenoticeDetailRepository)
        {
            _purchasenoticeDetailRepository = purchasenoticeDetailRepository;
        }

        /// <summary>
        /// 进货通知明细显示
        /// </summary>
        /// <param name="customername">客户名称</param>
        /// <param name="prductname">商品名称</param>
        /// <param name="page">初始页</param>
        /// <param name="limit">每页几条数据</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PagedResult> GetPurchaseNoticeDetailInfo(string? customername, string? prductname, int page, int limit)
        {
            var purchase = PredicateBuilder.New<PurchaseNoticeDetails>(true);
            if (!string.IsNullOrEmpty(customername))
            {
                purchase = purchase.And(m => m.CustomerName.Contains(customername));
            }
            if (!string.IsNullOrEmpty(prductname))
            {
                purchase = purchase.And(m => m.ProductName.Contains(prductname));
            }
            var purchaselist = _purchasenoticeDetailRepository.GetListAsync(purchase).Result;
            PagedResult result = new PagedResult();
            result.CurrentPage = page;//起始页
            result.PageSize = limit;//每页有几条数据
            result.PageCount = (int)Math.Ceiling(purchaselist.Count * 1.0 / limit);//总页数
            result.RowCount = purchaselist.Count;//一共有几条数据
            result.Queryable = purchaselist.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return result;
        }

        /// <summary>
        /// 进货通知明细添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<int> CreatePurchaseNotice(CreatePurchaseNoticeDTO dto)
        {
            var entity = ObjectMapper.Map<CreatePurchaseNoticeDTO, PurchaseNoticeDetails>(dto);
            await _purchasenoticeDetailRepository.InsertAsync(entity);
            return 1;
        }

        /// <summary>
        /// 进货通知明细删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> DeletePurchaseNotice(int id)
        {
            await _purchasenoticeDetailRepository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 根据Id获取进货通知明细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<GetPurchaseNoticeDTO> FanTianPurchaseDetail(int id)
        {
            var entity = _purchasenoticeDetailRepository.GetAsync(id).Result;
            var purchase = ObjectMapper.Map<PurchaseNoticeDetails, GetPurchaseNoticeDTO>(entity);
            return purchase;
        }

        /// <summary>
        /// 进货通知明细修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdatePurchaseNotice(UpdatePurchaseNoticeDTO dto)
        {
            var entity = ObjectMapper.Map<UpdatePurchaseNoticeDTO, PurchaseNoticeDetails>(dto);
            await _purchasenoticeDetailRepository.UpdateAsync(entity);
            return 1;
        }
    }
}
